package cn.oyjg;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

/**
 * 生成实体和映射文件
 * @author k11ha_000
 *
 */
public class Test {
	public static void main(String args[]){
		try{
			String[] tableName={"gameinfo"};
			String[] domainName={"GameInfo"};			
			String[] path={"D:\\workspace2\\gameService\\src\\cn\\oyjg\\sys\\"};	
			
			String[] packageName=new String[path.length];
			for(int i=0;i<packageName.length;i++){
				String tmp=path[i].substring(path[i].indexOf("src")+4); //{"cn.oyjg.sys.core"};
				tmp=tmp.replace("\\",".");
				packageName[i] =tmp.substring(0,tmp.length()-1);
			}
			String[] pk={"id"};
			for(int i=0;i<tableName.length;i++){
				create(tableName[i],domainName[i],packageName[i],path[i],pk[i]);
			}
		}catch(Exception err){
			err.printStackTrace();
		}
	}
	public static void create(String tableName,String domainName,String packageName,String path,String pk){
		try{
			String sql="SELECT column_name col,data_type ty,column_comment comments FROM information_schema.columns WHERE table_name='"+tableName+"' AND table_schema='gameListener'";
			Class.forName("com.mysql.jdbc.Driver");
			String url="jdbc:mysql://localhost:3306/gameListener?characterEncoding=utf8";
			Connection conn=DriverManager.getConnection(url,"root","jianguo");	
			
			StringBuilder sb=new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n"+
				"<!DOCTYPE mapper"+
				" PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\""+
				" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\" >\r\n"+
				"<mapper namespace=\""+packageName+".dao."+domainName+"Dao\">\r\n"+
				"<resultMap type=\""+domainName+"\" id=\""+domainName.substring(0,1).toLowerCase()+domainName.substring(1)+"\">\r\n");
		    
	    	//<result property="id" column="id"/>
			
			Statement stmt=conn.createStatement();
			ResultSet rs=stmt.executeQuery(sql);
			ArrayList<Table> fields=new ArrayList<Table>();
			while(rs.next()){
				//String field=rs.getString("column").substring(0,1).toLowerCase()+rs.getString("column").substring(1);
				String columnName=rs.getString("col");
				String[] fieldNameT=rs.getString("col").toLowerCase().split("_");
				String fieldName="";
				for(int i=0;i<fieldNameT.length;i++){
					if(i==0){
						fieldName+=fieldNameT[i];
					}else{
						fieldName+=fieldNameT[i].substring(0,1).toUpperCase()+fieldNameT[i].substring(1);
					}
					
				}
				sb.append("    <result property=\""+fieldName+"\" column=\""+columnName+"\" />\r\n");
				
				Table t=new Table(fieldName,columnName,rs.getString("ty"),rs.getString("comments"));
				fields.add(t);
			}
			sb.append("</resultMap>\r\n");
			sb.append("<sql id=\"mainColumns\">\r\n    ");
			String tmp="";
			for(int i=0;i<fields.size();i++){
				if(!fields.get(i).getColumnName().equals(pk)){
					tmp+=fields.get(i).getColumnName()+",";
				}
			}
			sb.append(tmp.substring(0,tmp.length()-1));		
			//
			sb.append("\r\n</sql>\r\n\r\n");
			tmp="";
			sb.append("<insert id=\"insert\" parameterType=\""+packageName+".entity."+domainName+"\" useGeneratedKeys=\"true\" keyProperty=\""+pk+"\" >\r\n");
			sb.append("    insert into "+tableName+"(<include refid=\"mainColumns\"/>)\r\n    values(");
			for(int i=0;i<fields.size();i++){
				if(!fields.get(i).getColumnName().equals(pk)){
					tmp+="#{"+fields.get(i).getColumnName()+"},";
				}
			}	
			sb.append(tmp.substring(0,tmp.length()-1));		
			sb.append(tmp);
			sb.append(")\r\n</insert>\r\n\r\n");
			//更新
			sb.append("<update id=\"update\" parameterType=\""+packageName+".entity."+domainName+"\" >\r\n");
			sb.append("    update "+tableName+" set ");
			for(int i=0;i<fields.size();i++){
				if(!fields.get(i).getColumnName().equals(pk)){
					tmp+=fields.get(i).getColumnName()+"=#{"+fields.get(i).getFieldName()+"},";
				}
			}
			sb.append(tmp.substring(0,tmp.length()-1));		
			sb.append("\r\n    where "+pk+"=#{"+pk+"}\r\n</update>\r\n\r\n");
			
			//删除
			sb.append("<delete id=\"delete\" parameterType=\"Integer\" >\r\n");
			sb.append("    delete from "+tableName+" where "+pk+"=#{_parameter} \r\n</delete>\r\n\r\n");
			
			//查询
			sb.append("<select id=\"selectAll\" resultMap=\""+domainName.substring(0,1).toLowerCase()+domainName.substring(1)+"\" >\r\n");
			sb.append("    select * from "+tableName+" \r\n</select>\r\n");
			//查询2
			sb.append("<select id=\"selectListByPage\" parameterType=\"cn.oyjg.base.util.PageUtil\" resultMap=\""+domainName.substring(0,1).toLowerCase()+domainName.substring(1)+"\" >\r\n");
			sb.append("    select * from "+tableName+" limit #{startRow},#{pagesize}"
					+ "\r\n</select>\r\n");
			//查询3
			sb.append("<select id=\"selectCountByPage\" parameterType=\"cn.oyjg.base.util.PageUtil\" resultType=\"int\" >\r\n");
			sb.append("    select count(*) from "+tableName+" "
					+ "\r\n</select>\r\n</mapper>");
			OutputStream os=new FileOutputStream(new File(path+"dao\\"+domainName+"Dao.xml"));
			os.write(sb.toString().getBytes());
			os.flush();
			os.close();
			//生成dao
			sb=new StringBuilder();
			sb.append("package "+packageName+".dao;\r\n");
			sb.append("import cn.oyjg.base.util.PageUtil;\r\n");
			sb.append("import cn.oyjg.base.dao.BaseDao;\r\n");
			sb.append("import "+packageName+".entity."+domainName+";\r\n");
			sb.append("public interface "+domainName+"Dao extends BaseDao<"+domainName+">{\r\n\r\n}  \r\n");
			os=new FileOutputStream(new File(path+"dao\\"+domainName+"Dao.java"));
			os.write(sb.toString().getBytes());
			os.flush();
			os.close();
			
			//生成实体
			sb=new StringBuilder();
			sb.append("package "+packageName+".entity;\r\n\r\n");
			//sb.append("import cn.oyjg.base.util.PageUtil;\r\n");
			sb.append("public class "+domainName+"{\r\n");
			tmp="";
			for(Table t:fields){			
				tmp+="\t/**\r\n\t*"+t.getComment()+"\t\r\n\t*/\r\n\tprivate "+t.getType()+" "+t.getFieldName()+";\r\n\r\n";
			}
			sb.append(tmp);
			sb.append("}  \r\n");
			os=new FileOutputStream(new File(path+"\\entity\\"+domainName+".java"));
			os.write(sb.toString().getBytes());
			os.flush();
			os.close();
			
			//生成service接口
			sb=new StringBuilder();
			sb.append("package "+packageName+".service;\r\n\r\n");
			sb.append("import cn.oyjg.base.service.BaseService;\r\n");
			sb.append("import "+packageName+".entity."+domainName+";\r\n\r\n");
			sb.append("public interface I"+domainName+"Service extends BaseService<"+domainName+">{\r\n");
			tmp="";
			sb.append(tmp);
			sb.append("}  \r\n");
			os=new FileOutputStream(new File(path+"\\service\\I"+domainName+"Service.java"));
			os.write(sb.toString().getBytes());
			os.flush();
			os.close();
			
			
			//生成service实现类
			sb=new StringBuilder();
			sb.append("package "+packageName+".service.impl;\r\n\r\n");
			sb.append("import cn.oyjg.base.dao.BaseDao;\r\n");
			sb.append("import cn.oyjg.base.service.BaseServiceImpl;\r\n");
			sb.append("import cn.oyjg.sys.dao."+domainName+"Dao;\r\n");
			sb.append("import "+packageName+".entity."+domainName+";\r\n");
			sb.append("import org.springframework.beans.factory.annotation.Autowired;\r\n");
			sb.append("import org.springframework.stereotype.Service;\r\n\r\n");
			
			sb.append("import "+packageName+".service.I"+domainName+"Service;\r\n");
			sb.append("@Service(\""+(domainName.substring(0,1).toLowerCase()+domainName.substring(1))+"Service\")\r\n");
			sb.append("public class "+domainName+"ServiceImpl extends BaseServiceImpl<"+domainName+"> implements I"+domainName+"Service {\r\n");
			tmp="\r\n\r\n"+
			"\tpublic BaseDao<"+domainName+"> getBaseDao() {\r\n"+
			""+
			"\t	return "+(domainName.substring(0,1).toLowerCase()+domainName.substring(1))+"Dao;"+
			"\r\n\t}"
			+ "\r\n\r\n\t@Autowired"+
			"\r\n\tprivate "+domainName+"Dao "+(domainName.substring(0,1).toLowerCase()+domainName.substring(1))+"Dao;\r\n";
			sb.append(tmp);
			sb.append("}\t  \r\n");
			os=new FileOutputStream(new File(path+"\\service\\impl\\"+domainName+"ServiceImpl.java"));
			os.write(sb.toString().getBytes());
			os.flush();
			os.close();
			System.out.println("完成");
			
		
		}catch(Exception err){
			err.printStackTrace();
		}
}
}



